package com.licheedev.adplayer;

import android.os.AsyncTask;
import com.kk.taurus.playerbase.entity.DataSource;
import com.licheedev.adplayer.cache.FileLruCache;
import com.licheedev.adplayer.loader.VideoDownLoader;
import java.io.File;
import java.io.IOException;

public interface DataSourceTask {

    void start();

    void cancel();

    interface Callback {

        void beforeGetDataSource();

        void getDataSourceSuccess(DataSource dataSource);

        void getDataSourceFailure();
    }

    // 边下边播缓存
    class VideoCacheTask implements DataSourceTask {

        private Callback mCallback;
        private final String mUrl;

        public VideoCacheTask(Callback callback, String url) {
            mCallback = callback;
            mUrl = url;
        }

        @Override
        public void start() {

            String proxyUrl = AdPlayerConfig.instance().getCacheProxyServer().getProxyUrl(mUrl);
            DataSource dataSource = new DataSource(proxyUrl);
            if (mCallback != null) {
                mCallback.getDataSourceSuccess(dataSource);
            }
        }

        @Override
        public void cancel() {
            mCallback = null;
        }
    }

    // 下载文件任务
    class LruDownTask extends AsyncTask<String, Void, File>
        implements DataSourceTask {

        private Callback mCallback;
        private final String mUrl;

        public LruDownTask(Callback callback, String url) {
            mCallback = callback;
            mUrl = url;
        }

        private FileLruCache getCache() {
            return AdPlayerConfig.instance().getLruCache();
        }

        @Override
        protected File doInBackground(String... strings) {

            FileLruCache cache = getCache();

            // 如果还是没有缓存，就直接返回空
            if (cache == null) {
                return null;
            }

            String url = strings[0];
            String mds = Util.md5(url);
            FileLruCache.Editor edit = null;

            try {
                edit = cache.edit(mds);
                VideoDownLoader videoDownLoader = AdPlayerConfig.instance().getVideoDownLoader();
                videoDownLoader.download(url, edit.newOutputFile());
                edit.commit();
            } catch (Exception e) {
                e.printStackTrace();
                if (edit != null) {
                    try {
                        edit.abort();
                    } catch (IOException ex) {
                        //ex.printStackTrace();
                    }
                }
            }

            try {
                cache.flush();
                // 立即尝试获取
                return cache.getFile(mds);
            } catch (IOException e) {
                //e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(File file) {
            if (mCallback != null) {
                if (file == null) {
                    mCallback.getDataSourceFailure();
                } else {
                    mCallback.getDataSourceSuccess(new DataSource(file.getAbsolutePath()));
                }
            }
        }

        @Override
        public void start() {

            String md5 = Util.md5(mUrl);
            FileLruCache cache = getCache();
            if (cache != null) {
                File file = cache.getFile(md5);
                if (file != null && mCallback != null) {
                    mCallback.getDataSourceSuccess(new DataSource(file.getAbsolutePath()));
                    cancel();
                    return;
                }
            }

            if (mCallback != null) {
                mCallback.beforeGetDataSource();
            }

            execute(mUrl);
        }

        @Override
        public void cancel() {
            mCallback = null;
            cancel(false);
        }
    }
}